package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.employment.entity.Company;
import com.wanmait.employment.entity.CompanyWelfareLabelBasics;
import com.wanmait.employment.entity.WelfareLabelBasics;
import com.wanmait.employment.mapper.CompanyMapper;
import com.wanmait.employment.mapper.CompanyWelfareLabelBasicsMapper;
import com.wanmait.employment.mapper.WelfareLabelBasicsMapper;
import com.wanmait.employment.service.CompanyWelfareLabelBasicsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class CompanyWelfareLabelBasicsServiceImpl implements CompanyWelfareLabelBasicsService {
   @Resource
   private CompanyWelfareLabelBasicsMapper companyWelfareLabelBasicsMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private WelfareLabelBasicsMapper welfareLabelBasicsMapper;


    @Override
    public PageInfo<CompanyWelfareLabelBasics> selectList(String title, Integer pageNum, Integer pageSize) {
        QueryWrapper<Company> companyQueryWrapper1 =new QueryWrapper<>();
        companyQueryWrapper1.like("company_name",title);
        List<Company> companyList = companyMapper.selectList(companyQueryWrapper1);
        ArrayList<Integer> companyIds = new ArrayList<>();
        companyList.forEach(company -> {
            companyIds.add(company.getId());
        });

        QueryWrapper<WelfareLabelBasics> welfareLabelBasicsQueryWrapper = new QueryWrapper<>();
        welfareLabelBasicsQueryWrapper.like("title",title);
        List<WelfareLabelBasics> welfareLabelBasicsList = welfareLabelBasicsMapper.selectList(welfareLabelBasicsQueryWrapper);
        ArrayList<Integer> welfareLabelBasicsIds = new ArrayList<>();
        welfareLabelBasicsList.forEach(welfareLabelBasics -> {
            welfareLabelBasicsIds.add(welfareLabelBasics.getId());
        });

        QueryWrapper<CompanyWelfareLabelBasics> queryWrapper=new QueryWrapper<>();
        queryWrapper.in(!companyIds.isEmpty(),"company_id",companyIds);
        queryWrapper.in(!welfareLabelBasicsList.isEmpty(),"welfare_label_basice_title_id",welfareLabelBasicsIds);
        System.out.println(title);
//        queryWrapper.like("company_name",title).or().like("title",title);
        PageHelper.startPage(pageNum,pageSize);
        List<CompanyWelfareLabelBasics> companyWelfareLabelBasicsList=companyWelfareLabelBasicsMapper.selectList(queryWrapper);
        PageInfo<CompanyWelfareLabelBasics> companyWelfareLabelBasicsPageInfo=new PageInfo<>(companyWelfareLabelBasicsList);
        companyWelfareLabelBasicsPageInfo.getList().forEach((companyWelfareLabelBasics) -> {

            QueryWrapper<Company> companyQueryWrapper=new QueryWrapper<>();
            companyQueryWrapper.eq("id",companyWelfareLabelBasics.getCompanyId());
            Company company=companyMapper.selectOne(companyQueryWrapper);
            companyWelfareLabelBasics.setCompany(company);

            QueryWrapper<WelfareLabelBasics> welfareLabelBasicsQueryWrapper1=new QueryWrapper<>();
            welfareLabelBasicsQueryWrapper1.eq("id",companyWelfareLabelBasics.getWelfareLabelBasiceTitleId());
            WelfareLabelBasics welfareLabelBasics=welfareLabelBasicsMapper.selectOne(welfareLabelBasicsQueryWrapper1);
            companyWelfareLabelBasics.setWelfareLabelBasics(welfareLabelBasics);

        });
        return companyWelfareLabelBasicsPageInfo;
    }

    @Override
    public Integer update(CompanyWelfareLabelBasics companyWelfareLabelBasics) {
        return companyWelfareLabelBasicsMapper.updateById(companyWelfareLabelBasics);
    }

    @Override
    public Integer insert(CompanyWelfareLabelBasics companyWelfareLabelBasics) {
        return companyWelfareLabelBasicsMapper.insert(companyWelfareLabelBasics);
    }

    @Override
    public void delete(Integer id) {
        companyWelfareLabelBasicsMapper.deleteById(id);

    }
}
